package com.neuedu.dao.Imp;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.neuedu.dao.FoodDao;
import com.neuedu.pojo.Business;
import com.neuedu.pojo.Food;
import com.neuedu.utils.JDBCUtil;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FoodDaoImp implements FoodDao {
    private DataSource dataSource = JDBCUtil.getDruidDataSource();
    @Override
    public Integer addFood(String foodName, String foodExplain, String foodImg, String foodPrice, String shopId, String remarks, String stock, String status, String categoryId, String recommend, String hot) throws Exception {
        return Db.use(dataSource).insert(
                Entity.create("food")
                        .set("food_name",foodName)
                        .set("food_explain",foodExplain)
                        .set("food_img",foodImg)
                        .set("food_price",foodPrice)
                        .set("shop_id",shopId)
                        .set("remarks",remarks)
                        .set("stock",stock)
                        .set("status",status)
                        .set("category_id",categoryId)
                        .set("recommend",recommend)
                        .set("hot",hot)
        );
    }

    @Override
    public Integer updateFood(String foodName, String foodExplain, String foodImg, String foodPrice, String remarks, String stock, String status, String categoryId, String foodId) throws Exception {
        return Db.use(dataSource).update(
                Entity.create()
                        .set("food_name",foodName)
                        .set("food_explain",foodExplain)
                        .set("food_img",foodImg)
                        .set("food_price",foodPrice)
                        .set("remarks",remarks)
                        .set("stock",stock)
                        .set("status",status)
                        .set("category_id",categoryId),
                Entity.create("food")
                        .set("food_id",foodId)
        );
    }

    @Override
    public Integer delFood(String foodId) throws Exception {
        return Db.use(dataSource).del(Entity.create("food").set("food_id",foodId));
    }

    @Override
    public List<Food> getFood(String currentPage, String pageSize,  String foodName, String categoryId, String shopId) throws Exception {
        String sql = "select * from food where shop_id = ? ";

        // 定义一个map集合，将搜索条件对应的列名作为map的key，搜索条件的值作为map的value
        Map<String,String> map = new HashMap<>();
        // 判断传递的参数
        if(StrUtil.isNotBlank(foodName)){
            map.put("foodName",foodName);
        }
        if(StrUtil.isNotBlank(categoryId)){
            map.put("categoryId",categoryId);
        }
        // 这里可以直接判断map是否为空
        if(!map.isEmpty()){
            sql += " and ";
            // 判断成立，说明map中有搜索的条件数据，这时只要遍历map，去给sql拼接条件即可
            Set<Map.Entry<String,String>> entries = map.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                sql += entry.getKey() + " like" + " '%" + entry.getValue() + "%' or ";
            }
            // sql的最后多or关键字 ，需要单独处理"select * from business where account like %?% or telephone like %?% or contact like %?% or "
            sql = sql.substring(0,sql.lastIndexOf("or"));
        }
        // "select * from business where account like %?% or telephone like %?% or contact like %?% limit 起始位置 , 条数"
        // 判断分页的参数、
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if(StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        sql += " limit " + (page - 1) * size + " , " + size;
        return Db.use(dataSource).query(sql, Food.class,shopId);
    }

    @Override
    public Long countFood(String shopId) throws Exception {
        return Db.use(dataSource).count(Entity.create("food").set("shop_id",shopId));
    }
}
